import 'package:flutter/material.dart';

import 'package:aweb5/base/BaseWidget.dart';


import 'package:flutter/cupertino.dart';
import 'package:aweb5/util/WindowUtil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:aweb5/config/Config.dart';
import 'package:dio/dio.dart';
import 'dart:async';
import 'package:aweb5/pages/WrongPage.dart';
import 'AplusHomePage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:aweb5/util/DialogUtil.dart';
class WrongPage extends BaseWidget {


//  Function func;
//  WrongPage(this.func);
  @override
  State<WrongPage> createState() => WrongPageState();
}

class WrongPageState extends BaseWidgetState<WrongPage> {
  Dio dio;
  String data = "1.在浏览使用时，我们会收集、使用设备标识信息用于推荐。\n2.我们可能会申请位置权限，用于帮助你在发布的信息中展示位置或丰富信息推荐维度。城市位置无需使用位置权限，仅通过ip地址确定“城市频道”中的城市及相关信息，不会收集精确位置信息。\n3.你可以查看完整版\n如果你同意请点击下面的按钮以接受我们的服务。";
  BuildContext context;
  @override
  void initState() {
    WindowUtil.setStatusBar(Colors.white, true);
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return Scaffold(
      appBar: appBar("花花牛优鲜汇"),
      body:
      Container(
        width: MediaQuery.of(context).size.width,
        child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Image.asset("assets/wuwangluo_.png",width: 170,height: 114,),
            SizedBox(height: 35,),
            Text("网络竟然崩溃了",style: TextStyle(color: Color(0xffFF5B00),fontSize: 16),),
            SizedBox(height: 17,),
            Text("别紧张，尝试下刷新页面哦~",style: TextStyle(color: Color(0xffFF5B00),fontSize: 12),),
            SizedBox(height: 35,),
            InkWell(
              child: Container(width: 149,height: 30,decoration: BoxDecoration(color: Color(0xffFF5B00),borderRadius: BorderRadius.circular(10)),
                child: Center(child: Text("刷新页面",style: TextStyle(color: Colors.white),),),
              ),
              onTap: (){
                get(Config.baseUrl, context);
              },
            )
          ],
        ),
      )
      ,
    );
  }

  Future<Map> get(String url,BuildContext context, {data, method}) async {
    data = data ?? {};
    method =   'GET';

    /// restful 请求处理
    /// /gysw/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /gysw/search/hist/27
//    data.forEach((key, value) {
//      if (url.indexOf(key) != -1) {
//        url = url.replaceAll(':$key', value.toString());
//      }
//    });

    /// 打印请求相关信息：请求地址、请求方式、请求参数
    print('请求地址：【' + method + '  ' + url + '】');
    print('请求参数：' + data.toString());

    Dio dio = createInstance();
    var result;

    try {
      Response response = await dio.request(url,
          options: new Options(method: method));
      Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (con){
//        return HomePage();
      }));
      // showDialog(context: context,builder: (context){return LoadingDialog(text: "");});
      //  result = response.data;

      /// 打印响应相关信息
      //  print('响应数据：' + response.toString());
    }  catch (e) {

      /// 打印请求失败相关信息
      print('请求出错：' + e.toString());
      Fluttertoast.showToast(msg: "请求出错");
      dio.reject(e);
      // rethrow;
    } finally{

    }

    return result;
  }



  Dio createInstance() {
    if (dio == null) {
      /// 全局属性：请求前缀、连接超时时间、响应超时时间
      BaseOptions options = new BaseOptions(
//        baseUrl: API_PREFIX,
        connectTimeout: 3000,
        receiveTimeout: 5000,
      );

      dio = new Dio(options);
    }

    return dio;
  }

  Future isFirst() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getBool("show");
  }
  setFirst()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool("show", false);
  }

}